<template>
  <div>
    <vxe-toolbar id="toolbarId_re9" :custom="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      highlight-hover-row
      row-id="id"
      height="700"
      highlight-current-row
      :loading="loading"
      :data="tableData"
      :sort-config="{ remote: true }"
      export-config
      show-overflow
      align="center"
      @sort-change="sortList"
      show-footer
      :footer-method="footerMethod"
    >
    <vxe-table-column type="checkbox" width="60"  fixed="left" />
      <vxe-table-column>
        <vxe-table-column
          field="authStatus"
          title="审核状态"
          :filter-multiple="false"
          :formatter="formatterAuthStatus"
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-select v-model="listQuery.authStatus" placeholder="请选择" clearable>
            <el-option
              v-for="item in authStatusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="userName"
          title="姓名"
          show-header-overflow
          show-overflow
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.userName" placeholder="姓名" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="phone"
          title="手机号"
          show-header-overflow
          show-overflow
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.phone" placeholder="手机号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="companyName"
          title="公司名称"
          show-header-overflow
          show-overflow
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.companyName" placeholder="公司名称" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="staffType"
          title="入驻商类型"
          :filter-multiple="false"
          :formatter="formatterStaffType"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-select v-model="listQuery.staffTypes" placeholder="请选择">
            <el-option
              v-for="item in staffTypeOptions2"
              :key="item.value"
              :label="item.label"
              :value="item.value+''"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="taxPoint"
          title="税点(%)"
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input type="text" v-model="listQuery.taxPointStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input type="text" v-model="listQuery.taxPointEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="amount"
          title="余额（元）"
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input type="text" v-model="listQuery.amountStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input type="text" v-model="listQuery.amountEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <!--<vxe-table-column>
        <vxe-table-column
          field="verifyCompany"
          title="企业验证通过"
          :formatter="formatterVerifyCompany"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-select v-model="listQuery.verifyCompany" clearable placeholder="请选择">
            <el-option
              v-for="item in verifyCompanyOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </vxe-table-column>-->
      <vxe-table-column v-if="isCtms">
        <vxe-table-column
          field="isIndependentCheckout"
          title="是否独立核算"
          :formatter="formatterCommonStatus"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-select v-model="listQuery.isIndependentCheckout" clearable placeholder="请选择">
            <el-option
              v-for="item in commonStatusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <!-- 创建时间 -->
      <vxe-table-column>
        <vxe-table-column
          field="gmtCreated"
          title="创建时间"
          min-width="420"
          sortable
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'gmtCreated')"
          />
        </template>
      </vxe-table-column>
      <!-- 审核时间 -->
      <vxe-table-column>
        <vxe-table-column
          field="authTime"
          title="审核时间"
          min-width="420"
          sortable
        />
        <template v-slot:header>
          <el-date-picker
            v-model="authTimeRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'authTime')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column field="action" title="操作" width="120" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="详情" placement="top">
            <vxe-button type="text" @click="funcDetail(scope.row.code)">
              <i class="el-icon-document" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="审核"
             v-if="scope.row.status == '0'"
            placement="top"
          >
            <vxe-button type="text" @click="funcRiview(scope.row.code)">
              <i class="el-icon-s-check" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <!-- 详情 -->
    <vxe-modal v-model="shipperDetail" title="详情" width="1200" height="680" resize>
      <shipperDetail ref="detailRef" :parent-data="parentData" />
    </vxe-modal>
    <!-- 审核 -->
    <vxe-modal v-model="shipperReview" title="审核" width="1200" height="760" resize>
      <shipperForm ref="reviewRef" :parent-data="parentData" @fatherMethods="fatherMethods" />
    </vxe-modal>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
  </div>
</template>
<script>
import shipperDetail from "./components/shipperDetail.vue";
import shipperForm from "./components/shipperForm.vue";
import { SelectAddress } from "@/api/orderform";
import { getShipperList, delShipperInfo } from "@/api/shipment";
import { getRole } from '@/utils/getRole';
export default {
  name: "ShipmentList",
  components: {
    shipperDetail,
    shipperForm
  },
  data() {
    return {
      isCtms:true,//默认显示  同城系统隐藏
      parentData: {
        isDetail: true,
        areaOptions: [],
        receiverUserCodeOptions: [],
        typeOptions: [],
        bankOptions: []
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      loading: false,
      tableData: [],
      gmtCreatedRange: "",
      authTimeRange: "",
      shipperDetail: false,
      shipperReview: false,
      listQuery: {
        userName: "", // 用户名
        phone: "", // 手机号
        companyName: "", // 公司名称
        freezingAmount: "", // 冻结金额
        amount: "", // 账户金额
        taxPoint: "", // 税点
        authStatus: 0, // 审核状态：0未审核/1审核中/2审核未通过/3审核通过
        gmtCreatedStart: "", // 创建时间起始
        gmtCreatedEnd: "", // 创建时间终止
        authTimeStart: "", // 审核时间开始
        authTimeEnd: "", // 审核时间结束
        isIndependentCheckout: "", // 是否独立核算:0否/1是
        verifyCompany: "", // 企业是否验证通过:0否/1是
        staffTypes: "1,0", // 角色，0-独立入驻商，1-企业入驻商，2-企业发货人，3-企业财务，4-企业收货人，5-上游客户，6-代付机构
        pageNum: 1,
        pageSize: 20,
        sortField: "",
        sortType: "",
        total: 0
      },
      Pagingconf: [],
      sumObject: {
        amount: 0
      },
      // 审核状态
      authStatusOptions: [
        {
          label: "未审核",
          value: 0
        },
        {
          label: "审核中",
          value: 1
        },
        { label: "审核未通过", value: 2},
        { label: "审核通过", value: 3}
      ],

      // 入驻商类型
      staffTypeOptions2: [
        {
          label: "独立入驻商",
          value: 0
        },
        {
          label: "企业入驻商",
          value: 1
        },
        {
          label: "全部",
          value: '1,0'
        },
      ],
      // 入驻商类型
      staffTypeOptions: [
        {
          label: "独立入驻商",
          value: 0
        },
        {
          label: "企业入驻商",
          value: 1
        },
        {
          label: "企业发货人",
          value: 2
        },
        {
          label: "企业财务",
          value: 3
        },
        {
          label: "企业收货人",
          value: 4
        },
        {
          label: "上游客户",
          value: 5
        },
        {
          label: "代付机构",
          value: 6
        }
      ],
      // 企业三要素状态
      verifyCompanyOptions: [
        {
          label: "未审核",
          value: 2
        },
        {
          label: "未通过",
          value: 0
        },
        {
          label: "通过",
          value: 1
        }
      ],
      // 通用是否状态
      commonStatusOptions: [
        {
          label: "否",
          value: 0
        },
        {
          label: "是",
          value: 1
        }
      ],
    };
  },
  watch:{
    $route(to,from) {
      const v = to.query.todo;
      if(v){
        this.listQuery.authStatus = 0;
      }else{
        this.listQuery.authStatus = '';
      }
      this.findList();
    }
  },
  created() {
    // 获取 省市县
    SelectAddress().then(response => {
      this.parentData.areaOptions = response.data.records;
    });
    const v = this.$route.query.todo;
    if(v){
      this.listQuery.authStatus = 0;
    }
    this.findList();
    let isCtmsShow = getRole();//获取登录用户登录的是否是同城系统
    this.isCtms = !isCtmsShow
  },
  methods: {
    // 时间
    changeDate(value, prefix) {
      if (value) {
        this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
        this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
      } else {
        this.listQuery[`${prefix}Start`] = "";
        this.listQuery[`${prefix}End`] = "";
      }
    },
    // 入驻商类型
    formatterStaffType({ cellValue }) {
      const item = this.staffTypeOptions.find(item => item.value === cellValue);
      return item ? item.label : "";
    },
    // 获取列表
    findList() {
      this.loading = true;
      return new Promise(resolve => {
        getShipperList(this.listQuery).then(response => {
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          // this.Pagingconf = [20,30,50,100,response.data.total];
          // this.sumObject = response.data.sumObject;
          this.loading = false;
          resolve();
        });
      });
    },
    // 获取详情
    funcDetail(code) {
      this.$refs.detailRef.getShipperDetail(code);
      this.shipperDetail = true;
    },
    // 审核
    funcRiview(code) {
      this.$refs.reviewRef.getShipperDetail(code);
      this.shipperReview = true;
    },
    // 关闭审核弹窗
    fatherMethods() {
      this.shipperReview = !this.shipperReview;
      this.findList();
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    },
    // 翻译审核状态
    formatterAuthStatus({ cellValue }) {
      if (cellValue == '0') {
        return '未审核'
      }
      if (cellValue == '1') {
        return '审核中'
      }
      if (cellValue == '2') {
        return '审核未通过'
      }
      if (cellValue == '3') {
        return '审核通过'
      }

    },
    // 翻译企业三要素状态
    formatterVerifyCompany({ cellValue }) {
      const item = this.verifyCompanyOptions.find(
        item => item.value === cellValue
      );
      return item ? item.label : "";
    },
    // 翻译通用是否状态
    formatterCommonStatus({ cellValue }) {
      const item = this.commonStatusOptions.find(
        item => item.value === cellValue
      );
      return item ? item.label : "";
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    },
    // 设置底部合计
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "合计";
          }
          if (column.property === "amount") {
            return this.sumObject.amount;
          }
          return "-";
        })
      ];
    },
  }
};
</script>
